package com.baosight.sczx.zm.cp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanCM;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanJJ;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanOther;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlan;

/**
 * 生产计划 - 月度计划
 * @author wealupa
 * By. 严洪浩
 */
public class ServiceZMCP09 extends ServiceEPBase {

	/**
	 * 初始化
	 * <p>Function: initLoad</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);		
		return outInfo;
	}
	
	/**
	 *  查询
	 * <p>Function: query</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdPlanYear = inInfo.getString("fdPlanYear");
		String fdPlanMonth = inInfo.getString("fdPlanMonth");
		StaticTools.setQueryParameters(inInfo, "fdPlanYear", fdPlanYear, true);
		StaticTools.setQueryParameters(inInfo, "fdPlanMonth", fdPlanMonth, true);
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		StaticTools.setQueryParameters(inInfo, "fdStatus", 1, true); 
		EiInfo outInfo = super.query(inInfo, "ZMCPProducePlan.planquery", new ZMCPProducePlan());
		return outInfo;
	}

	/**
	 * 新增生产计划主表
	 */
	@SuppressWarnings("rawtypes")
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		//校验是否重复
		String fdPlanYear = inInfo.getString("fdPlanYear");
		String fdPlanMonth = inInfo.getString("fdPlanMonth");
		
		EiInfo out = new EiInfo();
		ZMCPProducePlan _proPlan = new ZMCPProducePlan();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_proPlan.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_proPlan.fromMap(eiBlock.getRow(i));//获取数据
			Map<String, String>	_map = new HashMap<String, String>();
			String fdMiningId = UserSession.getInSessionProperty("miningCode").toString();
			
			_map.put("fdPlanYear", fdPlanYear);
			_map.put("fdPlanMonth", fdPlanMonth);
			_map.put("fdMiningId", fdMiningId);
			List validate = dao.query("ZMCPProducePlan.query", _map);
			
			if(validate != null && validate.size() > 0) {
				out = this.query(inInfo);
				out.setMsg(fdPlanYear + "年" + fdPlanMonth + "月的生产计划已经存在！");
				return out;
			}
			else {
				_proPlan.setFdPlanYear(fdPlanYear);
				_proPlan.setFdPlanMonth(fdPlanMonth);
				_proPlan.setFdEditDate(StaticTools.getDate());
				_proPlan.setFdEditor(UserSession.getInSessionProperty("UserCname").toString());
				_proPlan.setFdPlanSource(new Long(1));
				_proPlan.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				_proPlan.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				_proPlan.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN"));//主键
				this.dao.insert("ZMCPProducePlan.insert", _proPlan);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * 更新生产计划主表
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {				
		EiInfo out = new EiInfo(); 
		ZMCPProducePlan _dom = new ZMCPProducePlan();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdEditor(UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdEditDate(StaticTools.getDate());
			this.dao.update("ZMCPProducePlan.update", _dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;    
	}
	
	/**
	 * 发布生产计划
	 * @param inInfo
	 * @return
	 */
	public EiInfo report(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "fdPublisher", UserSession.getInSessionProperty("UserCname"));
		inInfo.getBlock("result").setCell(0, "fdPublishDate", StaticTools.getDate());
		inInfo.getBlock("result").setCell(0, "fdPlanStatus", "生效");
		EiInfo temp = super.update(inInfo, "ZMCPProducePlan.update");
		
		EiInfo outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg(temp.getMsg());
		return outInfo;  
	}
	
	/**
	 * 调整生产计划
	 * 1、首先必须是生效的计划，才能调整
	 * 2、
	 * @param inInfo
	 * @return
	 */
	public EiInfo adjust(EiInfo inInfo) {
		ZMCPProducePlan _dom = new ZMCPProducePlan();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		_dom.fromMap(eiBlock.getRow(0));
		String fdKeyId = _dom.getFdKeyId();
		
		//1、更改生产计划该记录为历史
		int result = this.dao.update("ZMCPProducePlan.changeToHistory",_dom);
		
		if (result > 0) {
			//2、复制一条记录，状态为草拟
			String newFdKeyId = TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN");//主键
			_dom.setFdKeyId(newFdKeyId);
			_dom.setFdPlanStatus("草拟");
			this.dao.insert("ZMCPProducePlan.insert",_dom);
			
			//3.采煤计划明细复制
			ProducePlanCM.cloneByAdjust(fdKeyId, newFdKeyId);
			
			//4.掘进计划明细复制
			ProducePlanJJ.cloneByAdjust(fdKeyId, newFdKeyId);
			
			//5.其他计划明细复制
			ProducePlanOther.cloneByAdjust(fdKeyId, newFdKeyId);
		}

		EiInfo outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		return outInfo;  
	}
}
